package com.tentact.coolmeetingtest.controller.servlet;
/**
 * @author 星环
 * @date 2023/1/10 22:01:03
 * @description
 */

import com.tentact.coolmeetingtest.annotation.RequestParam;
import com.tentact.coolmeetingtest.service.DeptService;
import com.tentact.coolmeetingtest.service.EmpService;
import com.tentact.coolmeetingtest.support.MyMessage;
import com.tentact.coolmeetingtest.support.RetMsg;
import com.tentact.coolmeetingtest.util.OtherUtil;
import com.tentact.coolmeetingtest.vo.Emp;
import com.tentact.coolmeetingtest.vo.EmpState;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.util.List;

public class EmpServlet {
    private EmpService empService = new EmpService();
    private DeptService deptService = new DeptService();

    public MyMessage delete(@RequestParam("empno") int empno) {
        RetMsg retMsg = empService.delete(empno);
        return MyMessage.of(true,"emp.approve","msg",retMsg.msg);
    }

    public MyMessage accept(@RequestParam("empno") int empno) {
        RetMsg retMsg = empService.accept(empno);
        return MyMessage.of(true,"emp.approve","msg",retMsg.msg);
    }

    public MyMessage approve(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Emp> emps = empService.getEmpByState(EmpState.WAITING.getState());
        return MyMessage.of(false,"approveaccount.jsp","emps",emps);
    }

    public MyMessage showall() {
        List<Emp> emps = empService.getAll();
        return MyMessage.of(true,"emp.searchForward");
    }

    public MyMessage login(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("username") String username, @RequestParam("password") String password,
                           @RequestParam("timelength") int time){
        RetMsg retMsg = empService.login(username, password);
        Emp emp = (Emp) retMsg.data;
        String destination = "login.jsp";
        if(retMsg.code == 0){
            destination = "adminindex.jsp";
            request.getSession().setAttribute("msg",username);
            System.out.println(request.getSession().getAttribute("msg"));
            int validTimeInSecond = time*24*60*60;
            response.addCookie(OtherUtil.getCookie("username",username,validTimeInSecond));
            response.addCookie(OtherUtil.getCookie("empno",String.valueOf(emp.getEmpno()), validTimeInSecond));
            response.addCookie(OtherUtil.getCookie("password",password,validTimeInSecond));
        }
        return MyMessage.of(false,destination,
                "msg",retMsg.msg);
    }

    public MyMessage register(Emp emp, @RequestParam("confirm_pwd") String confirm_pwd){
        if(emp.getDeptno() < 0){
            return MyMessage.of(true,"dept.regselect","msg","未选择部门");
        }
        RetMsg retMsg = empService.register(emp,confirm_pwd);
        return MyMessage.of(true,"emp.showall","msg",retMsg.msg);
    }

    public MyMessage searchForward(){
        return MyMessage.of(false,"searchEmployees.jsp",
                "emps",empService.searchEmpLimited(new Emp(),1,10),"maxCount",empService.getEmpCount(new Emp()));
    }


    private Emp lastSrch = new Emp();
    public MyMessage search(@RequestParam("now_page") int nowPage, @RequestParam("i_per_page") int iPerPage,
                            @RequestParam("new_search") String newSearch, Emp emp){

        if(newSearch != null){
            //重新获得lastSrch，即查询条件
            lastSrch = emp;
        }
        List<Emp> empList = empService.searchEmpLimited(lastSrch,nowPage,iPerPage);
        return MyMessage.of(false,"searchEmployees.jsp","emps",empList,"maxCount",empService.getEmpCount(lastSrch));
    }


    public MyMessage closeUser(@RequestParam("state") int state, @RequestParam("empno")int empno) throws ServletException,IOException{
        empService.setEmpStateByEmpno(empno,state);
        return MyMessage.of(false,"searchEmployees.jsp");
    }
}
